// pages/goodsSubClassify/goodsSubClassify.js
import { getStorage } from "../../utils/utils"
import {
	shareOptions
} from "../../utils/access";
import request from "../../utils/request";
const api = require("../../config/api");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude: "",
		longitude: "",
    isLoaing: false,
    goodsDesc:"",
    goodsClassifyId:0,
    goodsSubClassifyId:0,
    title:"",
    scrollTop: "",
    scrollTopFlag: true,
    isShowTag:false,
    list: [],
    page: 1,
    total: 0,
    isDone: false,
    communityId: getStorage('communityId')
  },
	//获取全部商品列表
	async getList(page) {
		const lat = getStorage("latitude");
    const lon = getStorage("longitude");
    const { province,  city, direct  } = getStorage('addressInfo') || {}
		const res = await request.get(api.allGoods, {
			page,
			size: 10,
			lat,
			lon,
      type: 4,
      goodsClassifyId:this.data.goodsClassifyId,
      goodsSubClassifyId:this.data.goodsSubClassifyId,
      provinceName: province || '',
      cityName: city|| '',
      districtName: direct || ''
    })

		this.setData({
			list: (page === 1 ? (res?.data?.records || []) : [
        ...this.data.list,
        ...res?.data?.records,
      ]).map(item => ({
        ...item,
        goodsSigns: (item.goodsSignList || item.goodsSigns)?.map(sign => sign.replace('#', ''))
      })),
      total: res?.data?.pages
		});

		wx.stopPullDownRefresh();
  },

  goGoodsSearch(){
    wx.navigateTo({
			url: '/pages/goodsSearch/goodsSearch',
		})
  },
  //点击回到顶部
  handleTop(){
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 2000
    })
  },
  //跳转详情
  handleGoodsDetail(event) {
		wx.navigateTo({
			url: `/pages/idleGosDetail/idleGosDetail?goodsId=` + event.currentTarget.id,
		});
	},
 
  onPageScroll(e) {
		let _this = this;
		let Top = e.scrollTop;
		_this.setData({
			scrollTop: Top
		})

		if (Top > 300) {
			_this.setData({
				scrollTopFlag: false
			})
		} else {
			_this.setData({
				scrollTopFlag: true
			})
		}
  },
  //点击回到顶部
	handleGoToTop() {
		wx.pageScrollTo({
			scrollTop: 0, // 滚动到页面的目标位置，单位 px
			duration: 500 // 滚动动画的时长，单位 ms
		})
	},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      goodsClassifyId:options.goodsClassifyId,
      goodsSubClassifyId:options.goodsSubClassifyId,
      title:options.title,
    });
    console.log("PPPPP",this.data.title);
    if (this.data.goodsSubClassifyId==0) {
      this.setData({
        isShowTag:true,
      });
    }
    this.setData({
      pageTitle: this.data.title,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList(1);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      isDone: false,
    })
    this.getList(1)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let { page, total } = this.data
    if (page < total) {
      page++
      this.setData({
        page
      })
      this.getList(page)
    } else {
      this.setData({
        isDone: true
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    return shareOptions('default', e)
  }
})